local mijing = fk.CreateSkill{
  name = "reminiscence__mijing",
}

local U = require "packages/utility/utility"

local mijing_pile = "reminiscence__liumin_jing"

Fk:loadTranslationTable{
  ["reminiscence__mijing"] = "弥旌",
  [":reminiscence__mijing"] = "当你的牌不因使用而置入弃牌堆后，你可将此牌置于武将牌上，称为“旌”；"
  .."当你需要使用或打出【闪】或【无懈可击】时，你可将手牌数摸至与“旌”数相同，然后移去一张“旌”并视为使用或打出之",

  [mijing_pile] = "旌",

  ["#reminiscence__mijing-remove"] = "弥旌：请移去一张“旌”",
  ["#reminiscence__mijing-put"] = "弥旌：将其中的任意张牌置为“旌”",
}

mijing:addEffect("viewas", {
  anim_type = "defensive",
  derived_piles = mijing_pile,
  pattern = "jink,nullification",
  interaction = function (self, player)
    local all_choices = {"jink", "nullification"}
    local choices = player:getViewAsCardNames(mijing.name, all_choices)
    return UI.CardNameBox { choices = choices, all_choices = all_choices, }
  end,
  card_filter = Util.FalseFunc,
  view_as = function (self, player, cards)
    local card = Fk:cloneCard(self.interaction.data)
    card.skillName = mijing.name
    return card
  end,
  before_use = function (self, player, use)
    local room = player.room
    local n = #player:getPile(mijing_pile) - player:getHandcardNum()
    player:drawCards(n, mijing.name)
    local card = room:askToChooseCard(player, {
      target = player,
      flag = {card_data = {{mijing.name, player:getPile(mijing_pile)},}},
      skill_name = mijing.name,
      prompt = "#reminiscence__mijing-remove",
    })
    room:moveCards{
      ids = {card},
      from = player,
      specialName = mijing_pile,
      toArea = Card.DiscardPile,
      proposer = player,
      moveReason = fk.ReasonPutIntoDiscardPile,
      moveVisible = true,
      skillName = mijing.name,
    }
  end,
  enabled_at_play = function (self, player)
    return player:getHandcardNum() < #player:getPile(mijing_pile)
  end,
  enabled_at_response = function (self, player, response)
    return player:getHandcardNum() < #player:getPile(mijing_pile)
  end
})

mijing:addEffect(fk.AfterCardsMove, {
  anim_type = "drawcard",
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(mijing.name) then
      local cards = {}
      for _, move in ipairs(data) do
        if move.from == player and move.toArea == Card.DiscardPile
        and move.moveReason ~= fk.ReasonUse then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea <= Card.PlayerEquip then
              table.insertIfNeed(cards, info.cardId)
            end
          end
        end
      end
      if #cards > 0 then
        event:setCostData(self, cards)
        return true
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    local cards = event:getCostData(self)
    local room = player.room
    cards = room:askToChooseCards(player, {
      min = 0,
      max = 999,
      flag = {card_data = { {mijing.name, cards}, }},
      skill_name = mijing.name,
      target = player,
      prompt = "#reminiscence__mijing-put",
    })
    if #cards > 0 then
      event:setCostData(self, cards)
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    player:addToPile(mijing_pile, event:getCostData(self), true, mijing.name, player)
  end
})

return mijing